package com.theydream.ichingwisdom.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;

import com.theydream.ichingwisdom.R;

public class DivinationResult3 extends Activity {
	
	private int num1,num2,num3;
	private ImageView card;
	private ImageView card2;
	private ImageView card3;
	private ImageView cardback;
	private ImageView cardback2;
	private ImageView cardback3;
	private ImageView taichi;
	private Animation anim;
	private boolean isFirstImage = true;
	private boolean isFirstImage2 = true;
	private boolean isFirstImage3 = true;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.divinationresult3);

		Bundle bData = this.getIntent().getExtras();
		num1 = bData.getInt("randomNumber");
		num2 = bData.getInt("randomNumber2");
		num3 = bData.getInt("randomNumber3");
		
		//Set taichi
				taichi = (ImageView)findViewById(R.id.taichi);
				taichi.setOnClickListener(new OnClickListener(){
					@Override
					public void onClick(View v) {
						Animation taichirotate = AnimationUtils.loadAnimation(DivinationResult3.this, R.anim.taichirotate);
						taichirotate.setAnimationListener(new AnimationListener() {         
							public void onAnimationStart(final Animation arg0) {
								taichi.setOnClickListener(null);
							}           
							public void onAnimationRepeat(final Animation arg0) {}          
							public void onAnimationEnd(final Animation rg0){
								taichi.setVisibility(View.GONE);
								cardback.startAnimation(anim);
								cardback2.startAnimation(anim);
								cardback3.startAnimation(anim);
							}
						});
						taichi.startAnimation(taichirotate);
					}
				});
		
		//Set cardback
		cardback = (ImageView) findViewById(R.id.cardBack);
		cardback.setVisibility(View.INVISIBLE);
		cardback2 = (ImageView) findViewById(R.id.cardBack2);
		cardback2.setVisibility(View.INVISIBLE);
		cardback3 = (ImageView) findViewById(R.id.cardBack3);
		cardback3.setVisibility(View.INVISIBLE);

		//Set card
		String uri = "drawable/card" + num1;
		int imageResource = getResources().getIdentifier(uri, null,
				getPackageName());
		card = (ImageView) findViewById(R.id.cardView);
		Drawable image = getResources().getDrawable(imageResource);
		card.setImageDrawable(image);
		
		String uri2 = "drawable/card" + num2;
		int imageResource2 = getResources().getIdentifier(uri2, null,
				getPackageName());
		card2 = (ImageView) findViewById(R.id.cardView2);
		Drawable image2 = getResources().getDrawable(imageResource2);
		card2.setImageDrawable(image2);
		
		String uri3 = "drawable/card" + num3;
		int imageResource3 = getResources().getIdentifier(uri3, null,
				getPackageName());
		card3 = (ImageView) findViewById(R.id.cardView3);
		Drawable image3 = getResources().getDrawable(imageResource3);
		card3.setImageDrawable(image3);
		
		card.setVisibility(View.INVISIBLE);
		card2.setVisibility(View.INVISIBLE);
		card3.setVisibility(View.INVISIBLE);
		
		//Set Animation
		anim = AnimationUtils.loadAnimation(this, R.anim.cardanim);
		anim.setAnimationListener(new AnimationListener() {         
		    public void onAnimationStart(final Animation arg0) {}           
		    public void onAnimationRepeat(final Animation arg0) {}          
		    public void onAnimationEnd(final Animation rg0){
		    	cardback.setVisibility(View.VISIBLE);
				cardback2.setVisibility(View.VISIBLE);
				cardback3.setVisibility(View.VISIBLE);
		    	cardback.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						if (isFirstImage) {       
							applyRotation(0, 90,card,cardback,isFirstImage);
							isFirstImage = !isFirstImage;
						}else {    
							GlobalData.setShowCard(num1-1);
					    	Intent intent=new Intent(DivinationResult3.this,CardDetail.class);
					        startActivity(intent);
						}
					}
					
				});
		    	cardback2.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						if (isFirstImage2) {       
							applyRotation(0, 90,card2,cardback2,isFirstImage2);
							isFirstImage2 = !isFirstImage2;
						}else {    
							GlobalData.setShowCard(num2-1);
					    	Intent intent=new Intent(DivinationResult3.this,CardDetail.class);
					        startActivity(intent);
						}
					}
					
				});
		    	cardback3.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						if (isFirstImage3) {       
							applyRotation(0, 90,card3,cardback3,isFirstImage3);
							isFirstImage3 = !isFirstImage3;
						}else {    
							GlobalData.setShowCard(num3-1);
					    	Intent intent=new Intent(DivinationResult3.this,CardDetail.class);
					        startActivity(intent);
						}
					}
					
				});
		    }
		});

	}
	private void applyRotation(float start, float end, ImageView c, ImageView cb, boolean isFirstImage) {
		// Find the center of image
		final float centerX = cb.getWidth() / 2.0f;
		final float centerY = cb.getHeight() / 2.0f;

		// Create a new 3D rotation with the supplied parameter
		// The animation listener is used to trigger the next animation
		final Flip3dAnimation rotation =
				new Flip3dAnimation(start, end, centerX, centerY);
		rotation.setDuration(500);
		rotation.setFillAfter(true);
		rotation.setInterpolator(new AccelerateInterpolator());
		rotation.setAnimationListener(new DisplayNextView(isFirstImage, cb, c));
		
		if (isFirstImage)
		{
			cb.startAnimation(rotation);
		} else {
			c.startAnimation(rotation);
		}

	}
}
